
package com.jf.cloud.supplier.controller.supplier;


import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.dto.SupplierCompanyDTO;
import com.jf.cloud.supplier.service.CompanyAuditingService;
import com.jf.cloud.supplier.vo.SupplierCompanyAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @date 2022/9/13 14:06
 */
@RestController("supplierCompanyAuditingController")
@RequestMapping("/s/supplier_company_auditing")
@Tag(name = "supplier-供应商变更工商信息")
public class CompanyAuditingController {

    @Autowired
    private CompanyAuditingService companyAuditingService;

    @Autowired
    private MapperFacade mapperFacade;

    @PostMapping("/apply_change_company_info")
    @Operation(summary = "申请变更工商信息" , description = "申请变更工商信息")
    public ServerResponseEntity<Void> applyChangeCompanyInfo(@RequestBody @Valid SupplierCompanyDTO supplierCompanyDTO) {
        Long supplierId = AuthUserContext.get().getTenantId();
        // 校验是否已经存在申请中记录
        SupplierCompanyAuditingVO latestAuditing = companyAuditingService.getLatestAuditingBySupplierId(supplierId);
        if (Objects.nonNull(latestAuditing) && Objects.equals(latestAuditing.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("不能同时提交多次签约申请，请耐心等候平台审核，或者把上次提交申请撤销后重新申请");
        }
        supplierCompanyDTO.setSupplierId(supplierId);
        companyAuditingService.applyChangeCompanyInfo(supplierCompanyDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/revoke")
    @Operation(summary = "撤销申请" , description = "撤销申请")
    public ServerResponseEntity<Void> revoke() {
        Long supplierId = AuthUserContext.get().getTenantId();
        companyAuditingService.revoke(supplierId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/auditInfo")
    @Operation(summary = "查看申请审核情况" , description = "查看申请审核情况")
    public ServerResponseEntity<SupplierCompanyAuditingVO> auditInfo() {
        Long supplierId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(companyAuditingService.getAuditInfo(supplierId));
    }
}
